package controller;

import GUI.LoginGUI;
import GUI.MerchantGUI;
import GUI.UserGUI;
import buyers.Buyer;
import buyers.BuyersDataRead;
import buyers.BuyersDataWrite;
import goods.Goods;
import goods.GoodsDataRead;
import goods.GoodsDataWrite;
import merchants.Merchant;
import merchants.MerchantDataRead;
import order.Order;
import order.OrderDataRead;
import order.OrderDataWrite;

import javax.swing.*;
import java.io.IOException;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class Controller {
    private static BuyersDataRead buyersDataRead = new BuyersDataRead();
    private static MerchantDataRead merchantDataRead = new MerchantDataRead();
    private static LoginGUI loginGUI = new LoginGUI();
    private static GoodsDataRead goodsDataRead = new GoodsDataRead();
    private static OrderDataRead orderDataRead = new OrderDataRead();
    private static UserGUI userGUI;
    private static String userName;

    public static void main(String[] args) throws IOException {
        BuyersDataRead.main();
        MerchantDataRead.main();
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            userName = loginGUI.getUserName();
            String passWord = loginGUI.getPassWord();
            int flag = 0;
            if(loginGUI.getFlag() == 1&&loginGUI.getPassWord() != null&&loginGUI.getUserName() != null){
                if(merchantDataRead.getMerchants().equal(userName, passWord)){
                    loginGUI.offInter();
                    createMerchantGUI();
                    flag = 1;
                    break;
                }else{
                    //账号或密码错误
                    JOptionPane.showMessageDialog(null,"用户名或密码错误！");
                    loginGUI.offInter();
                    return;
                }
            }else if(loginGUI.getFlag() == 2 && loginGUI.getPassWord() != null && loginGUI.getUserName() != null){
                if(buyersDataRead.getBuyersData().equal(userName, passWord)) {
                    loginGUI.offInter();
                    createUserGUI();
                    OrderDataRead.main();

                    flag = 2;
                    break;
                }else{
                    JOptionPane.showMessageDialog(null,"用户名或密码错误！");
                    loginGUI.offInter();
                    return;
                }
            }
        }
    }


    public static void createUserGUI(){
        userGUI = new UserGUI();
        GoodsDataRead.main();
    }

    public  ArrayList<String> findUserGUI(){
        String searchStr = userGUI.getSearchString();
        ArrayList<Goods> goodsArrayList = goodsDataRead.getGoodsData().findGoods(searchStr);
        ArrayList<String> strings = new ArrayList<>();
        if (goodsArrayList.size() == 0) {
            strings.add("无法查找此商品！");
        }else {
            for (Goods G : goodsArrayList) {
                StringBuilder stringBuilder = new StringBuilder();
                   stringBuilder.append(G.getId());
                   stringBuilder.append(" ").append(G.getName());
                   stringBuilder.append(" ").append(G.getPrice());
                   stringBuilder.append(" ").append(G.getAmount());
                   String str = new String(stringBuilder);
                   strings.add(str);
            }
        }
        return strings;

    }

    public void addCartArrayList(ArrayList<String> arrayList){
        buyersDataRead.getBuyersData().addCartData(arrayList,userName);
        BuyersDataWrite buyersDataWrite = new BuyersDataWrite();
        buyersDataWrite.DataWrite(buyersDataRead);
    }

    public ArrayList<String> getCartArrayList(){
        return buyersDataRead.getBuyersData().getUserCartArrayList(userName);
    }

    public void deleteCartArrayList(ArrayList<String> arrayList){
        buyersDataRead.getBuyersData().changeCart(arrayList,userName);
        BuyersDataWrite buyersDataWrite = new BuyersDataWrite();
        buyersDataWrite.DataWrite(buyersDataRead);
    }
    public String settlementShoppingCart(){
        String str = buyersDataRead.getBuyersData().checkOut(userName,goodsDataRead,orderDataRead);
        deleteCartArrayList(null);
        GoodsDataWrite goodsDataWrite = new GoodsDataWrite();
        goodsDataWrite.DataWrite(goodsDataRead);
        OrderDataWrite orderDataWrite = new OrderDataWrite();
        orderDataWrite.DataWrite(orderDataRead);
        return str;
    }
    public ArrayList<String> getOderArrayList(){
        return orderDataRead.getOrders().getOrderStringArrayList();
    }

    public static void createMerchantGUI(){
        MerchantGUI merchantGUI = new MerchantGUI();
        goodsDataRead.main();
    }

    public ArrayList<String> merchantGoodsDataArrayList(){
        ArrayList<String> arrayList = new ArrayList<>();
        for(Merchant M:merchantDataRead.getMerchants().getMerchantArrayList()){
            if(M.getUserName().equals(userName)){
                int i = 0 ;
                for (Long l:M.getMerchant()) {
                    for(;i<goodsDataRead.getGoodsData().getGoodsArrayList().size();i++){
                        if(l == goodsDataRead.getGoodsData().getGoodsArrayList().get(i).getId()){
                            StringBuilder str = new StringBuilder();
                            str.append(goodsDataRead.getGoodsData().getGoodsArrayList().get(i).getId()+" "+goodsDataRead.getGoodsData().getGoodsArrayList().get(i).getName()+" "+
                                    goodsDataRead.getGoodsData().getGoodsArrayList().get(i).toStringLabel()+goodsDataRead.getGoodsData().getGoodsArrayList().get(i).getPrice()+" "+
                                    goodsDataRead.getGoodsData().getGoodsArrayList().get(i).getAmount());
                            arrayList.add(new String(str));
                            break;
                        }
                    }
                }
                break;
            }
        }
        System.out.println("111");
        return arrayList;
    }


}


